package com.acme;

import com.acme.hash.SmearIntHashFunctionImpl;
import com.acme.hashmap.DirectAddressedHashMapIntToInt;
import com.acme.hashmap.LinkedHashMapIntToInt;
import com.acme.hashmap.MapIntToInt;

import java.util.Collections;
import java.util.HashMap;

/**
 * @author Gregory Sobko
 */
public class MemoryMain {
    public static void main(String[] args) throws InterruptedException {

        //Warmup
        {
            long initial = Runtime.getRuntime().freeMemory();
            MapIntToInt map = new LinkedHashMapIntToInt(new SmearIntHashFunctionImpl());
            for (int i = 0; i < 100000; i++) {
                map.put(i, 2 * i);
            }
            Runtime.getRuntime().gc();
            System.out.println(initial - Runtime.getRuntime().freeMemory());
            System.err.println(map.get(PerfMain.randInt(0, 90000)));
        }

        //Hash Map
        {
            long initial = Runtime.getRuntime().freeMemory();
            HashMap<Integer,Integer> map = new HashMap<Integer, Integer>();
            for (int i = 0; i < 100000; i++) {
                map.put(i, 2 * i);
            }
            System.out.println(initial - Runtime.getRuntime().freeMemory());
            System.err.println(Collections.max(map.keySet()));
        }
        Runtime.getRuntime().gc();
        Thread.sleep(1000);
        Runtime.getRuntime().gc();
        //Our Hash Map
        {
            long initial = Runtime.getRuntime().freeMemory();
            MapIntToInt map = new LinkedHashMapIntToInt(new SmearIntHashFunctionImpl());
            for (int i = 0; i < 100000; i++) {
                map.put(i, 2 * i);
            }
            Runtime.getRuntime().gc();
            System.out.println(initial - Runtime.getRuntime().freeMemory());
            System.err.println(map.get(PerfMain.randInt(0, 90000)));
        }
        Runtime.getRuntime().gc();
        Thread.sleep(100);
        //Our Direct Addressed Hash Map
        {
            long initial = Runtime.getRuntime().freeMemory();
            MapIntToInt map = new DirectAddressedHashMapIntToInt(new SmearIntHashFunctionImpl());
            for (int i = 0; i < 100000; i++) {
                map.put(i, 2 * i);
            }
            Runtime.getRuntime().gc();
            System.out.println(initial - Runtime.getRuntime().freeMemory());
            System.err.println(map.get(PerfMain.randInt(0, 90000)));
        }
        Runtime.getRuntime().gc();
        Thread.sleep(100);
    }
}
